<?php

/*
 * 本地店铺管理
 * 2016.10.19
 */

namespace Qwadmin\Controller;


class PartnerController  extends ComController{
    public function index()
    {
        $p = isset($_GET['p']) ? intval($_GET['p']) : '1';
        $m = M('partner');
        $pagesize = 10; #每页数量
        $offset = $pagesize * ($p - 1); //计算记录偏移量
        $count = $m->where($where)->count();
        $list = $m->where($where)->limit($offset . ',' . $pagesize)->select();
        $tradeList=D("Trade")->getTradList();
        $this->assign('tradeList',$tradeList);
        $page = new \Think\Page($count, $pagesize);
        $page = $page->show();
        $this->assign('list', $list);
        $this->assign('page', $page);
        $this->display();
    }
    
    
    /*
  * 添加本地店铺
  * */
    public function add()
    {
        $tradeList = M('trade')->group('code')->select();
        $this->assign('tradeList',$tradeList);
        $this->display('form');
    }

    /*
    * 删除本地店铺
    * */
    public function del()
    {

        $ids = isset($_REQUEST['ids']) ? $_REQUEST['ids'] : false;

        if (is_array($ids)) {
            foreach ($ids as $k => $v) {
                $ids[$k] = intval($v);
            }
        }elseif(!$ids){
            $this->error('参数错误！');
            }

        $map['id'] = array('in', $ids);
        if (M('partner')->where($map)->delete()) {
            addlog('删除店铺ID：' . $ids);
            $this->success('恭喜，店铺删除成功！');
        } else {
            $this->error('参数错误！');
        }
    }

  
    /*
    * 修改本地店铺
    * */  
    public function edit($id = 0)
    {
        $id = intval($id);
        $m = M('partner');
        $currentmenu = $m->where("id='$id'")->find();
        if (!$currentmenu) {
            $this->error('参数错误！');
        }

        $option = $m->select();
        $tradeList = M('trade')->group('code')->select();
        $this->assign('tradeList',$tradeList);
        $this->assign('option', $option);
        $this->assign('currentmenu', $currentmenu);
        $this->display('form');
    }

    public function update()
    {
        $m = M('partner');
        $id = I('post.id', '', 'intval');
        $data['name'] = I('post.name', '', 'strip_tags');
        $data['trade'] = I('post.trade');
        $data['owner'] = I('post.owner');
        $data['tel'] = I('post.tel');
        $data['status'] = I('post.status');
        $data['exp_time'] = I('post.exp_time');
    
        if ($id) {
            if(M('partner')->where(array('name'=> $data['name'],"id"=>array('neq',$id)))->find()){
                $this->error('该店铺已经存在！');
            }
            M('partner')->data($data)->where("id='{$id}'")->save();
            addlog('编辑店铺，ID：' . $id);
        } else {
                if(M('partner')->where(array('name'=> $data['name']))->find()){
                    $this->error('该店铺已经存在！');
                }
                M('partner')->data($data)->add();
                addlog('新增店铺，名称：' . $data['name']);
        }

        $this->success('操作成功！','index');
    }
}
